package com.snap.awesomeserial.ui.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.snap.awesomeserial.R;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class DialogView {
    private Context context;

    private TextButton sureBtn;
    private TextButton cancelBtn;
    private TextButton warnBtn;
    private TextView titleTv;


    private View layout;
    private AlertDialog dialog;
    private OnSureListener listener;
    private OnWarnListener warnListener;
    private OnCancelListener cancelListener;

    private boolean isCanceledOnTouchOutside = true;


    public DialogView(Context context) {
        this.context = context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = inflater.inflate(R.layout.dialog_view, null);
        sureBtn = layout.findViewById(R.id.sure_btn);
        cancelBtn = layout.findViewById(R.id.cancel_btn);
        warnBtn = layout.findViewById(R.id.warn_btn);
        titleTv = layout.findViewById(R.id.title_tv);
        dialog = builder.create();
    }


    public DialogView setOnClickListener(String content, OnCancelListener cancelListener) {
        this.cancelListener = cancelListener;
        if (cancelBtn != null) {
            cancelBtn.setText(content);
        }
        return this;
    }

    public DialogView setOnClickListener(String content, OnSureListener sureListener) {
        this.listener = sureListener;
        if (sureBtn != null && warnBtn != null) {
            warnBtn.setVisibility(View.GONE);
            sureBtn.setVisibility(View.VISIBLE);
        }
        if (sureBtn != null) {
            sureBtn.setText(content);
        }
        return this;
    }

    public DialogView setOnClickListener(String content, OnWarnListener warnListener) {
        this.warnListener = warnListener;
        if (sureBtn != null && warnBtn != null) {
            sureBtn.setVisibility(View.GONE);
            warnBtn.setVisibility(View.VISIBLE);
        }
        if (warnBtn != null) {
            warnBtn.setText(content);
        }
        return this;
    }

    public DialogView setTitleContent(String content) {
        if (titleTv != null) {
            titleTv.setText(content);
        }
        return this;
    }


    public void show() {
        dialog.show();
        dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside);
        Window window = dialog.getWindow();
        if (window != null) {
            //自定义布局应该在这里添加，要在dialog.show()的后面
            window.setContentView(layout);
            //允许弹出系统键盘
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            window.setLayout(AutoSizeUtils.dp2px(context, 408), AutoSizeUtils.dp2px(context, 200));
            //设置背景颜色
            window.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.bg_dialog));
        }

        sureBtn.setOnClickListener(v -> {
            if (listener != null)
                listener.onSureClick();
        });
        warnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (warnListener != null)
                    warnListener.onWarnClick();
            }
        });
        cancelBtn.setOnClickListener(v -> {
            dialog.dismiss();
            if (cancelListener != null) {
                cancelListener.onCancelClick();
            }
        });

    }


    public interface OnSureListener {
        void onSureClick();
    }

    public interface OnWarnListener {
        void onWarnClick();
    }

    public interface OnCancelListener {
        void onCancelClick();
    }
}
